#include<vector>
#include<algorithm>
using namespace std;

//leetcode611

class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int sz = nums.size();
        int sum = 0;
        for (int i = sz - 1; i > 1; --i)
        {
            int left = 0, right = i - 1;
            while (left < right)
            {
                if (nums[left] + nums[right] > nums[i])
                {
                    sum += right - left;
                    --right;
                }
                else
                    ++left;
            }
        }
        return sum;
    }
};